package com.easylinkin.linkappapi.circuit.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.circuit.dto.RailLinkappAiHostRecordDTO;
import com.easylinkin.linkappapi.circuit.mapper.RailLinkappAiHostRecordMapper;
import com.easylinkin.linkappapi.circuit.service.IRailGiveAlarmTypeConfigService;
import com.easylinkin.linkappapi.circuit.service.IRailLinkappAiHostRecordService;
import com.easylinkin.linkappapi.circuit.vo.RailLinkappAiHostRecordVO;
import com.easylinkin.linkappapi.circuit.vo.RailLinkappAiHostRecordStatisticsVO;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.device.service.DeviceService;
import com.easylinkin.linkappapi.mechanical.entity.EquipmentLocation;
import com.easylinkin.linkappapi.mechanical.mapper.EquipmentLocationMapper;
import com.easylinkin.linkappapi.mechanical.vo.EquipmentLocationVo;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.*;

/**
 * AI综合安防接口类
 * @author lyk
 * @date 2025/6/23 下午 7:48
 */
@RestController
@RequestMapping("/aiHostRecord")
public class RailLinkappAiHostRecordController {

    @Autowired
    private IRailLinkappAiHostRecordService railLinkappAiHostRecordService;
    @Autowired
    private LinkappUserContextProducer linkappUserContextProducer;

    @Autowired
    private EquipmentLocationMapper equipmentLocationMapper;

    @Autowired
    private DeviceService deviceService;

    /**
     * AI行为告警数量统计接口
     * @return
     */
    @GetMapping(value = "/aiBehaviorAlerts")
    public RestMessage aiBehaviorAlerts(){
        Map<String,Object> result = railLinkappAiHostRecordService.aiBehaviorAlerts();
        return RestBuilders.successBuilder(result).build();
    }

    @GetMapping(value = "/getViolationBehaviorPieChart")
    public RestMessage getViolationBehaviorPieChart(){
        Map<String,Object> result = railLinkappAiHostRecordService.getViolationBehaviorPieChart();
        return RestBuilders.successBuilder(result).build();
    }



    /**
     * 获取当然抓拍总数折线图统计接口
     * @return
     */
    @GetMapping(value = "/nowDaySnapTotleLineChart")
    public RestMessage nowDaySnapTotleLineChart(){
        Map<String,Object> result = railLinkappAiHostRecordService.nowDaySnapTotleLineChart();
        return RestBuilders.successBuilder(result).build();
    }

    /**
     * 获取统计数据接口（当日统计数量、近7日统计数量）
     * @return
     */
    @GetMapping(value = "/getStatistics")
    public RestMessage getStatistics(){
        RailLinkappAiHostRecordStatisticsVO result = railLinkappAiHostRecordService.getStatistics();
        return RestBuilders.successBuilder(result).build();
    }

    /**
     * AI告警统计接口
     * 返回今日告警总次数、比较昨日、近一个月总次数、累计总次数
     * @return
     */
    @GetMapping(value = "/getAiAlarmStatistics")
    public RestMessage getAiAlarmStatistics(){
        Map<String, Object> result = railLinkappAiHostRecordService.getAiAlarmStatistics();
        return RestBuilders.successBuilder(result).build();
    }

    /** 列表查询接口
     * @param dto
     * @return
     */
    @PostMapping(value = "/getList")
    public RestMessage getList(@RequestBody RailLinkappAiHostRecordDTO dto){
        List<RailLinkappAiHostRecordVO> list = railLinkappAiHostRecordService.getList(dto);
        return RestBuilders.successBuilder(list).build();
    }

    /** 分页查询接口
     * @param requestModel
     * @return
     */
    @PostMapping(value = "/selectPage")
    public RestMessage selectPage(@RequestBody RequestModel<RailLinkappAiHostRecordDTO> requestModel){
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<RailLinkappAiHostRecordVO> record = railLinkappAiHostRecordService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 大屏获取当前ai安防流水信息最新的记录
     * @return
     */
    @GetMapping(value = "/getNowByOne")
    public RestMessage getNowByOne(){
        RailLinkappAiHostRecordVO res  = railLinkappAiHostRecordService.getNowByOne();
        List<RailLinkappAiHostRecordVO> arrList = new ArrayList<>();
        if (!Objects.isNull(res)){
            arrList.add(res);
        }
        return RestBuilders.successBuilder(arrList).build();
    }

    /**
     * 用电安全大屏查找地图的用电设备，返回设备相关的数据
     */
    @GetMapping(value = "/geoMapAiHostDevice")
    public RestMessage geoMapUseEleDevice(){
       String tenantId= linkappUserContextProducer.getNotNullCurrent().getTenantId();
        EquipmentLocationVo vo = new  EquipmentLocationVo();
        List<String> nameTypes = new ArrayList<>();
        nameTypes.add("AI识别主机");
        vo.setTypeNames(nameTypes);
        vo.setTenantId(tenantId);
        List<EquipmentLocation> equipmentLocations = equipmentLocationMapper.listPage(vo);
        //图片获取
        List<String> codes = new ArrayList<>();
        for(EquipmentLocation eq:equipmentLocations){
           String code =  eq.getEquipmentCode();
           if (!StringUtils.isEmpty(code)){
               codes.add(code);
           }
        }
        if (codes.size()>0){
            Map<String, String> img_iMap = new HashMap<>();
            List<RailLinkappAiHostRecordVO> railLinkappAiHostRecordVOS = railLinkappAiHostRecordService.codeByNewRecords(tenantId, codes);
            for (RailLinkappAiHostRecordVO vos:railLinkappAiHostRecordVOS){
                if (StringUtils.isNotEmpty(vos.getDeviceCode()) && StringUtils.isNotEmpty(vos.getImage()) ){
                    img_iMap.put(vos.getDeviceCode(),vos.getImage());
                }
            }
            if (img_iMap.size()>0){
                for (EquipmentLocation eq:equipmentLocations){
                    String s = img_iMap.get(eq.getEquipmentCode());
                    if (StringUtils.isNotEmpty(s)){
                        eq.setAiRecordImg(s);
                    }
                }
            }
        }
        return RestBuilders.successBuilder().data(equipmentLocations).build();
    }

    /**
     * @return
     * DType:1是天7天
     * DType:2是30天
     */
    @PostMapping(value = "/byStatisticsLins")
    public RestMessage byStatisticsLins(@RequestBody RailLinkappAiHostRecordDTO dto){
        if (StringUtils.isEmpty(dto.getType())){
            return RestBuilders.failureMessage("统计范围字段不能为空");
        }
        String tenantId= linkappUserContextProducer.getNotNullCurrent().getTenantId();
        List<String> types = new ArrayList<>();
        types.add("AI识别主机");
        Device device = new Device();
        device.setDeviceTypeNames(types);
        device.setTenantId(tenantId);
        List<Device> devices = deviceService.selectByTypeNameDevicesList(device);
        Map<String,Object> res = railLinkappAiHostRecordService.byStatisticsLins(tenantId,devices,dto);
        return RestBuilders.successBuilder().data(res).build();
    }

    /**AI安防大屏提示语获取接口
     * @return
     */
    @GetMapping(value = "/getPrompt")
    public RestMessage getPrompt(){
        String tenantId= linkappUserContextProducer.getNotNullCurrent().getTenantId();
        Map<String,Object> res = railLinkappAiHostRecordService.getPrompt(tenantId);
        return RestBuilders.successBuilder().data(res).build();
    }



}
